package com.travel.service.impl.destination;

import com.github.pagehelper.PageHelper;
import com.travel.exception.ResourceNotFoundException;
import com.travel.mapper.content.CommentMapper;
import com.travel.mapper.content.DestinationMapper;
import com.travel.model.dto.comment.CommentRequest;
import com.travel.model.dto.comment.RatingStats;
import com.travel.model.dto.destination.DestinationDTO;
import com.travel.model.entity.content.Comment;
import com.travel.model.entity.service.Destination;
import com.travel.service.Service.DestinationService;
import com.travel.util.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;
//目的地服务实现类 获取所有公共景点 获取特定城市景点 获取特定景点 创建景点 更新景点 删除景点 搜索景点 添加评论 获取评论 获取评分统计
@Service
@RequiredArgsConstructor
public class DestinationServiceImpl implements DestinationService {

    private final DestinationMapper destinationMapper;
    private final CommentMapper commentMapper;

    @Override
    public Page<DestinationDTO> getAllPublicDestinations(Pageable pageable) {
        PageHelper.startPage(pageable.getPageNumber() + 1, pageable.getPageSize());
        List<Destination> destinations = destinationMapper.findByStatus(1);
        List<DestinationDTO> dtos = destinations.stream()
            .map(this::convertToDTO)
            .collect(Collectors.toList());
        return new PageImpl<>(dtos);
    }

    @Override
    public List<DestinationDTO> getDestinationsByCity(String city) {
        List<Destination> destinations = destinationMapper.findByCityAndStatus(city, 1);
        return destinations.stream()
            .map(this::convertToDTO)
            .collect(Collectors.toList());
    }

    @Override
    public DestinationDTO getDestinationById(Long id) {
        Destination destination = destinationMapper.findById(id)
            .orElseThrow(() -> new ResourceNotFoundException("景点不存在"));
        return convertToDTO(destination);
    }

    @Override
    @Transactional
    public DestinationDTO createDestination(DestinationDTO destinationDTO) {
        Destination destination = new Destination();
        BeanUtils.copyProperties(destinationDTO, destination);
        destinationMapper.save(destination);
        return convertToDTO(destination);
    }

    @Override
    @Transactional
    public DestinationDTO updateDestination(Long id, DestinationDTO destinationDTO) {
        Destination destination = destinationMapper.findById(id)
            .orElseThrow(() -> new ResourceNotFoundException("景点不存在"));
        
        BeanUtils.copyProperties(destinationDTO, destination, "id", "createdTime");
        destinationMapper.save(destination);
        return convertToDTO(destination);
    }

    @Override
    @Transactional
    public void deleteDestination(Long id) {
        Destination destination = destinationMapper.findById(id)
            .orElseThrow(() -> new ResourceNotFoundException("景点不存在"));
        destinationMapper.delete(destination);
    }

    @Override
    public Page<DestinationDTO> searchDestinations(String keyword, Pageable pageable) {
        PageHelper.startPage(pageable.getPageNumber() + 1, pageable.getPageSize());
        List<Destination> destinations = destinationMapper.searchDestinationsByStatus(keyword, 1);
        List<DestinationDTO> dtos = destinations.stream()
            .map(this::convertToDTO)
            .collect(Collectors.toList());
        return new PageImpl<>(dtos);
    }

    @Override
    @Transactional
    public Comment addComment(Long destinationId, CommentRequest request) {
        Destination destination = destinationMapper.findById(destinationId)
            .orElseThrow(() -> new ResourceNotFoundException("目的地不存在"));
        
        Comment comment = new Comment();
        comment.setContent(request.getContent());
        comment.setRating(request.getRating());
        comment.setUserId(SecurityUtils.getCurrentUserId());
        comment.setTargetId(destinationId);
        comment.setTargetType(Comment.TargetType.DESTINATION);
        comment.setCreatedTime(LocalDateTime.now());
        comment.setStatus(1);
        
        commentMapper.insert(comment);
        return comment;
    }

    @Override
    public List<Comment> getComments(Long destinationId) {
        return commentMapper.findByTargetAndStatus(destinationId, Comment.TargetType.DESTINATION, 1);
    }

    @Override
    public RatingStats getDestinationRating(Long destinationId) {
        RatingStats stats = new RatingStats();
        stats.setAverageRating(commentMapper.getAverageRating(
            destinationId, Comment.TargetType.DESTINATION));
        stats.setCommentCount(commentMapper.getCommentCount(
            destinationId, Comment.TargetType.DESTINATION));
        return stats;
    }

    private DestinationDTO convertToDTO(Destination destination) {
        DestinationDTO dto = new DestinationDTO();
        BeanUtils.copyProperties(destination, dto);
        
        // 添加评分信息
        RatingStats stats = getDestinationRating(destination.getId());
        dto.setAverageRating(stats.getAverageRating());
        dto.setCommentCount(stats.getCommentCount());
        
        return dto;
    }
} 